$OpenBSD: patch-machdep_bsd-44_c,v 1.2 2010/01/19 15:25:21 okan Exp $

--- machdep/bsd-44.c.orig	Thu Apr  4 23:17:21 2002
+++ machdep/bsd-44.c	Mon Jan 18 16:49:30 2010
@@ -38,12 +38,55 @@ static char rcsid[] = "$Id: bsd-44.c,v 1.17 2002/03/21
 #endif
 #endif
 
+#if defined(BSD4_4) && (BSD < 199306)
+#include <sys/kinfo.h>
+#include <sys/kinfo_proc.h>
+#define	_ALLPROCFLAG	KINFO_PROC_ALL
+#else
+#include <sys/sysctl.h>
+#include <limits.h>
+#define	_ALLPROCFLAG	KERN_PROC_ALL
+#endif
+
 #include <sys/resource.h>
+#include <err.h>
+#include <signal.h>
 #include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
 #include <string.h>
-#include <signal.h>
+#include <unistd.h>
 
+#ifndef	P_WEXIT		/* backward compatibility */
+#define	P_WEXIT	SWEXIT
+#endif
+
+static	char	*pidmap[] = { "swapper", "init", "pagedaemon" };
+static	int	pidmapsiz = sizeof(pidmap) / sizeof(pidmap[0]);
+
+#ifdef KERN_PROC2
+#define	CMDLEN			KI_MAXCOMLEN
+#define	PROCTYPE		struct kinfo_proc2
+#define	SYSCTL_TO_USE		KERN_PROC2
+#define	SYSCTL_MIBS		6
+#define	PROC_COMM(kprocp)	kprocp->p_comm
+#define	PROC_FLAG(kprocp)	kprocp->p_flag
+#define	PROC_PID(kprocp)	kprocp->p_pid
+#define	PROC_STAT(kprocp)	kprocp->p_stat
+#define	PROC_TDEV(kprocp)	kprocp->p_tdev
+#define	PROC_UID(kprocp)	kprocp->p_uid
+#else
+#define	PROCTYPE		struct kinfo_proc
+#define	SYSCTL_TO_USE		KERN_PROC
+#define	SYSCTL_MIBS		4
+#define	PROC_COMM(kprocp)	kprocp->kp_proc.p_comm
+#define	PROC_FLAG(kprocp)	kprocp->kp_proc.p_flag
+#define	PROC_PID(kprocp)	kprocp->kp_proc.p_pid
+#define	PROC_STAT(kprocp)	kprocp->kp_proc.p_stat
+#define	PROC_TDEV(kprocp)	kprocp->kp_eproc.e_tdev
+#define	PROC_UID(kprocp)	kprocp->kp_eproc.e_ucred.cr_uid
+#endif
+
+
 /*
  * Define SigNames, NSig, and TtyDevDir here; they are used by other
  * routines and must be global.  Everyone seems to have their own
@@ -150,33 +193,6 @@ MdepAction(pid)
 NULL_REGEX_FUNCS
 
 /*
- * Now, set up everything we need to write a GetProc() routine.
- */
-
-#include <kvm.h>
-#include <fcntl.h>
-
-#if defined(BSD4_4) && (BSD < 199306)
-#include <sys/kinfo.h>
-#include <sys/kinfo_proc.h>
-#define	_ALLPROCFLAG	KINFO_PROC_ALL
-#else
-#include <sys/sysctl.h>
-#include <limits.h>
-#define	_ALLPROCFLAG	KERN_PROC_ALL
-#endif
-
-#ifndef	P_WEXIT		/* backward compatibility */
-#define	P_WEXIT	SWEXIT
-#endif
-
-static	char	*pidmap[] = { "swapper", "init", "pagedaemon" };
-static	int	pidmapsiz = sizeof(pidmap) / sizeof(pidmap[0]);
-
-#define	PROC(kprocp)	kprocp->kp_proc
-#define	EPROC(kprocp)	kprocp->kp_eproc
-
-/*
  * GetProc()
  *
  * Fill in and return a `struct ProcInfo' with information about the
@@ -187,54 +203,54 @@ GetProc()
 {
 	static struct ProcInfo procinfo;
 	static int nproc = -1;
-	static struct kinfo_proc *aproc;
-	static kvm_t *kd = NULL;
-#ifdef	_POSIX2_LINE_MAX
-	char errbuf[_POSIX2_LINE_MAX];
-#else
-	char errbuf[2048];
-#endif
+	static PROCTYPE *aproc;
+	int mib[6];
+	size_t size;
 
 	/*
 	 * If this is our first time here, prepare to read procs from kernel.
 	 */
 	if (nproc == -1) {
-		kd = kvm_openfiles((char *)NULL, (char *)NULL, (char *)NULL,
-		                   O_RDONLY, errbuf);
-		if (kd == NULL) {
-			fprintf(stderr, "%s: %s\n", ProgName, errbuf);
-			exit(EX_SERR);
-		}
+		mib[0] = CTL_KERN;
+		mib[1] = SYSCTL_TO_USE;
+		mib[2] = _ALLPROCFLAG;
+		mib[3] = 0;
+#ifdef KERN_PROC2
+		mib[4] = sizeof(PROCTYPE);
+		mib[5] = 0;
+#endif
+		if (sysctl(mib, SYSCTL_MIBS, NULL, &size, NULL, 0) < 0)
+			err(1, "sysctl kern.proc2");
+		aproc = (PROCTYPE *)malloc(size);
+		if (aproc == NULL)
+			errx(1, "out of memory");
 
-		if ((aproc=kvm_getprocs(kd, _ALLPROCFLAG, 0, &nproc)) == NULL) {
-			fprintf(stderr, "%s: %s\n", ProgName, kvm_geterr(kd));
-			exit(EX_SERR);
-		}
+#ifdef KERN_PROC2
+		mib[5] = size / sizeof(PROCTYPE);
+#endif
+		if (sysctl(mib, SYSCTL_MIBS, aproc, &size, NULL, 0) < 0)
+			err(1, "sysctl kern.proc2");
+		nproc = size / sizeof(PROCTYPE);
 	}
 
-	if (nproc == 0) {
-		if (kd != NULL) {
-			kvm_close(kd);
-			kd = NULL;
-		}
-		return((struct ProcInfo *)NULL);
-	}
+	if (nproc == 0)
+		return(NULL);
 
 	do {
-		if (PROC(aproc).p_stat != 0) {
+		if (PROC_STAT(aproc) != 0) {
 			/*
 			 * Make sure this isn't a "zombie" or "exiting"
 			 * process.  If it is, fill in procinfo and return.
 			 */
 			procinfo.pi_flags = 0;
-			procinfo.pi_pid = (pid_T) PROC(aproc).p_pid;
-			procinfo.pi_uid = (uid_T) EPROC(aproc).e_ucred.cr_uid;
+			procinfo.pi_pid = (pid_T) PROC_PID(aproc);
+			procinfo.pi_uid = (uid_T) PROC_UID(aproc);
 
-			if (PROC(aproc).p_stat == SZOMB) {	/* zombie */
+			if (PROC_STAT(aproc) == SZOMB) {	/* zombie */
 				static char *zombie = "<defunct>";
 				procinfo.pi_flags |= PI_ZOMBIE;
 				procinfo.pi_cmd = zombie;
-			} else if (PROC(aproc).p_flag & P_WEXIT) { /* exiting */
+			} else if (PROC_FLAG(aproc) & P_WEXIT) { /* exiting */
 				static char *exiting = "<exiting>";
 				procinfo.pi_flags |= PI_SWEXIT;
 				procinfo.pi_cmd = exiting;
@@ -246,22 +262,22 @@ GetProc()
 				return(&procinfo);
 			}
 		}
-	} while (PROC(aproc).p_stat == 0);
+	} while (PROC_STAT(aproc) == 0);
 
 	/*
 	 * We now have a process (`aproc').
 	 * Fill in the rest of `procinfo'.
 	 */
-	if (EPROC(aproc).e_tdev != NODEV) {	/* controlling tty */
+	if (PROC_TDEV(aproc) != NODEV) {	/* controlling tty */
 		procinfo.pi_flags |= PI_CTLTTY;
-		procinfo.pi_tty = (tty_T) EPROC(aproc).e_tdev;
+		procinfo.pi_tty = (tty_T) PROC_TDEV(aproc);
 	}
 
-	if (PROC(aproc).p_pid < pidmapsiz) {	/* special */
-		procinfo.pi_cmd = pidmap[PROC(aproc).p_pid];
+	if (PROC_PID(aproc) < pidmapsiz) {	/* special */
+		procinfo.pi_cmd = pidmap[PROC_PID(aproc)];
 		procinfo.pi_flags |= PI_ASKUSR;
 	} else				 /* set path-stripped command name */
-		SETCMD(procinfo.pi_cmd, PROC(aproc).p_comm, MAXCOMLEN)
+		SETCMD(procinfo.pi_cmd, PROC_COMM(aproc), MAXCOMLEN)
 
 	nproc--;
 	aproc++;
